package com.scs.application.modules.order.excel.handler;
/**
 * @Description:
 */

import cn.afterturn.easypoi.handler.impl.ExcelDataHandlerDefaultImpl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.utils.Reflections;
import com.scs.application.core.utils.SpringHolder;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.order.entity.PurchaseOrder;
import com.scs.application.modules.order.service.PurchaseOrderService;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;

/**
 * @ClassName PurchaseOrderDataHandler
 * @Description TODO
 */
public class PurchaseOrderDataHandler extends ExcelDataHandlerDefaultImpl {
    private Map<String, PurchaseOrder> codeMap = Maps.newHashMap();

    public PurchaseOrderDataHandler() {
        List<PurchaseOrder> purchaseOrders = this.getPuraseOrderService().list(
                Wrappers.<PurchaseOrder>query().select(new String[]{"id,code"}).eq("hospital_id", UserUtils.getHospitalId())
        );
        for (PurchaseOrder purchaseOrder : purchaseOrders) {
            this.codeMap.put(purchaseOrder.getCode(), purchaseOrder);
        }

    }

    public String[] getNeedHandlerFields() {
        return new String[]{"订单编号"};
    }

    public Object importHandler(Object obj, String name, Object value) {
        if (!"订单编号".equalsIgnoreCase(name)) {
            return null;
        }
        if (value == null || StringUtils.isBlank(value.toString())) {
            return null;
        }
        PurchaseOrder purchaseOrder = this.codeMap.get(value.toString());
        if (purchaseOrder != null && Reflections.getAccessibleField(obj, "id") != null) {
            Reflections.setFieldValue(obj, "id", purchaseOrder.getId());
        }
        return null;

    }


    public PurchaseOrderService getPuraseOrderService() {
        return SpringHolder.getBean(PurchaseOrderService.class);
    }


}
